package com.gitee.controller.user;

import com.gitee.constant.MessageConstant;
import com.gitee.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.util.UUID;

@RequestMapping("/user/commonController")
@RestController
@Api(tags = "通用接口")
@Slf4j
public class CommonController {

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws FileUploadException {
        log.info("文件上传:{}", file);
        if (file.isEmpty()) {
            throw new FileUploadException("文件为空");
        }

        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        String url;
        try {
            // 获取文件名字+后缀
            String originalFilename = file.getOriginalFilename();
            // 拼接classpath:/static路径
            URL classPath = getClass().getResource("/");
            if (classPath == null) {
                throw new NullPointerException("路径为空");
            }
            String staticPath = classPath.getPath() + "static";

            // 若不存在改路径, 则创建
            File f = new File(staticPath);
            if (!f.exists()) {
                if (!f.mkdirs()) {
                    throw new IOException("新建文件夹失败");
                }
            }

            // UUID + 文件名, 放置文件重名
            originalFilename = UUID.randomUUID() + originalFilename;
            // 拼接路径: static/图片.jpg
            File destFile = new File(staticPath + "/" + originalFilename);

            // 进行文件输出
            bos = new BufferedOutputStream(new FileOutputStream(destFile));
            byte[] bytes = new byte[1024 * 10];
            bis = new BufferedInputStream(file.getInputStream());
            int readCount;
            while ((readCount = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, readCount);
            }

            // 最后返回给前端的地址
            url = "http://localhost:8081/" + originalFilename;
            return Result.success(url);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
